package org.monitoringtools.elements.impl;

import static org.monitoringtools.util.AssertUtil.assertNotNull;

import org.monitoringtools.elements.Counter;

public class BasicCounter implements Counter {

    private String id;

    protected long failure = 0;

    protected long success = 0;

    public BasicCounter(final String id) {
        assertNotNull(id);
        this.id = id;
    }

    public synchronized long failure() {
        return failure;
    }

    public String getId() {
        return id;
    }

    public synchronized void log(final boolean isSuccess) {
        incrementCounter(isSuccess);
    }

    public synchronized void reset() {
        this.success = 0;
        this.failure = 0;
    }

    public synchronized long success() {
        return success;
    }

    public synchronized long total() {
        return this.success + this.failure;
    }

    protected synchronized void incrementCounter(final boolean isSuccess) {
        if (isSuccess)
            this.success++;
        else
            this.failure++;
    }
}
